package com.exsun.ex1077.ui.popup

import android.content.Context
import android.widget.TextView
import com.amap.api.maps.AMap
import com.exsun.ex1077.R
import com.exsun.ex1077.app.Constant
import com.exsun.ex1077.model.MapPoi
import com.lxj.xpopup.core.BottomPopupView
import com.zhangping.amap.AMapHelper
import com.zhangping.fastjetpack.binding.gone
import com.zhangping.fastjetpack.binding.visible
import com.zhangping.fastjetpack.extend.showToast
import com.zhangping.fastjetpack.extend.toColorRes
import com.zhangping.fastjetpack.extend.toJson
import kotlinx.android.synthetic.main.activity_device_add.view.*
import kotlinx.android.synthetic.main.popup_map_menu.view.*

/**
 * 描述：地图菜单
 * 作者：ZhangPing
 * 创建时间：2023/3/15 17:44
 */
class MapMenuPopup(
    context: Context,
    private val mapHelper: AMapHelper,
    private val onSelectChangeList: (HashMap<Int, ArrayList<Int>>) -> Unit,
    private val onPolygonChangeList: (Boolean) -> Unit,
    private val onLineChangeList: (Boolean) -> Unit,
) : BottomPopupView(context) {

    override fun getImplLayoutId() = R.layout.popup_map_menu
    private val vehicleTagViewList: ArrayList<TextView> = arrayListOf()
    private val shipTagViewList: ArrayList<TextView> = arrayListOf()
    private val personTagViewList: ArrayList<TextView> = arrayListOf()
    private val animalTagViewList: ArrayList<TextView> = arrayListOf()
    private val objectTagViewList: ArrayList<TextView> = arrayListOf()
    private val otherTagViewList: ArrayList<TextView> = arrayListOf()
    private val boundsTagViewList: ArrayList<TextView> = arrayListOf()
    private val lineTagViewList: ArrayList<TextView> = arrayListOf()
    private val allTagViewList = arrayListOf(
        vehicleTagViewList,
        shipTagViewList,
        personTagViewList,
        animalTagViewList,
        objectTagViewList,
        otherTagViewList
    )
    private var objectList: ArrayList<Int>? = null

    override fun onCreate() {
        close.setOnClickListener { dismiss() }
        mapTypeNormal.setOnClickListener {
            //普通地图
            mapTypeNormal.setBackgroundResource(R.drawable.shape_radius5_bd_theme)
            mapTypeSatellite.setBackgroundResource(R.drawable.shape_empty)
            mapHelper.map.mapType = AMap.MAP_TYPE_NORMAL
            mapHelper.applyCustomStyle(R.raw.style, R.raw.style_extra)
        }
        mapTypeSatellite.setOnClickListener {
            //卫星地图
            mapTypeNormal.setBackgroundResource(R.drawable.shape_empty)
            mapTypeSatellite.setBackgroundResource(R.drawable.shape_radius5_bd_theme)
            mapHelper.map.mapType = AMap.MAP_TYPE_SATELLITE
        }
        vehicleTagViewList.addAll(arrayListOf(
            vehicleAll.apply { tag = true },
            vehicleRun.apply { tag = false },
            vehicleStop.apply { tag = false },
            vehicleFlameout.apply { tag = false },
            vehicleLocationFail.apply { tag = false },
            vehicleOffline.apply { tag = false })
        )
        bindingTagGroupClickListener(vehicleTagViewList)
        shipTagViewList.addAll(arrayListOf(
            shipAll.apply { tag = true },
            shipRun.apply { tag = false },
            shipStop.apply { tag = false },
            shipOffline.apply { tag = false },
            shipLocationFail.apply { tag = false }
        ))
        bindingTagGroupClickListener(shipTagViewList)
        personTagViewList.addAll(arrayListOf(
            personAll.apply { tag = true },
            personOnline.apply { tag = false },
            personOffline.apply { tag = false },
            personLocationFail.apply { tag = false }
        ))
        bindingTagGroupClickListener(personTagViewList)
        animalTagViewList.addAll(arrayListOf(
            animalAll.apply { tag = true },
            animalOnline.apply { tag = false },
            animalOffline.apply { tag = false },
            animalLocationFail.apply { tag = false }
        ))
        bindingTagGroupClickListener(animalTagViewList)
        objectTagViewList.addAll(arrayListOf(
            objectAll.apply { tag = true },
            objectOnline.apply { tag = false },
            objectOffline.apply { tag = false },
            objectLocationFail.apply { tag = false }
        ))
        bindingTagGroupClickListener(objectTagViewList)
        otherTagViewList.addAll(arrayListOf(
            otherAll.apply { tag = true },
            otherOnline.apply { tag = false },
            otherOffline.apply { tag = false },
            otherLocationFail.apply { tag = false }
        ))
        bindingTagGroupClickListener(otherTagViewList)
        boundsShow.tag = false
        boundsHide.tag = true
        bindingTagSwitchClickListener(true, boundsShow, boundsHide)
        lineShow.tag = false
        lineHide.tag = true
        bindingTagSwitchClickListener(false, lineShow, lineHide)
        initTagUI()
    }

    /**
     * 获取选中的监管对象
     */
    private fun getSelectedTagList(): HashMap<Int, ArrayList<Int>> {
        val selectedTagList = HashMap<Int, ArrayList<Int>>()
        //车辆筛选
        if (vehicleTagViewList[0].tag as Boolean) {
            //车辆选中全部
            selectedTagList.put(
                Constant.OBJECT_TYPE_VEHICLE, arrayListOf(
                    Constant.ONLINE_STATUS_RUN,
                    Constant.ONLINE_STATUS_STOP,
                    Constant.ONLINE_STATUS_FLAMEOUT,
                    Constant.ONLINE_STATUS_LOCATION_FAIL,
                    Constant.ONLINE_STATUS_OFFLINE,
                )
            )
        } else {
            val list = arrayListOf<Int>()
            if (vehicleTagViewList[1].tag as Boolean) {
                //行驶
                list.add(Constant.ONLINE_STATUS_RUN)
            }
            if (vehicleTagViewList[2].tag as Boolean) {
                //停车
                list.add(Constant.ONLINE_STATUS_STOP)
            }
            if (vehicleTagViewList[3].tag as Boolean) {
                //熄火
                list.add(Constant.ONLINE_STATUS_FLAMEOUT)
            }
            if (vehicleTagViewList[4].tag as Boolean) {
                //定位失败
                list.add(Constant.ONLINE_STATUS_LOCATION_FAIL)
            }
            if (vehicleTagViewList[5].tag as Boolean) {
                //离线
                list.add(Constant.ONLINE_STATUS_OFFLINE)
            }
            selectedTagList.put(Constant.OBJECT_TYPE_VEHICLE, list)
        }
        //船舶筛选
        if (shipTagViewList[0].tag as Boolean) {
            //船舶选中全部
            selectedTagList.put(
                Constant.OBJECT_TYPE_SHIP, arrayListOf(
                    Constant.ONLINE_STATUS_RUN,
                    Constant.ONLINE_STATUS_STOP,
                    Constant.ONLINE_STATUS_OFFLINE,
                    Constant.ONLINE_STATUS_LOCATION_FAIL
                )
            )
        } else {
            val list = arrayListOf<Int>()
            if (shipTagViewList[1].tag as Boolean) {
                //行驶
                list.add(Constant.ONLINE_STATUS_RUN)
            }
            if (shipTagViewList[2].tag as Boolean) {
                //停车
                list.add(Constant.ONLINE_STATUS_STOP)
            }
            if (shipTagViewList[3].tag as Boolean) {
                //离线
                list.add(Constant.ONLINE_STATUS_OFFLINE)
            }
            if (shipTagViewList[4].tag as Boolean) {
                //定位失败
                list.add(Constant.ONLINE_STATUS_LOCATION_FAIL)
            }
            selectedTagList.put(Constant.OBJECT_TYPE_SHIP, list)
        }
        //人员筛选
        if (personTagViewList[0].tag as Boolean) {
            //船舶选中全部
            selectedTagList.put(
                Constant.OBJECT_TYPE_PERSON, arrayListOf(
                    Constant.ONLINE_STATUS_ONLINE,
                    Constant.ONLINE_STATUS_OFFLINE,
                    Constant.ONLINE_STATUS_LOCATION_FAIL
                )
            )
        } else {
            val list = arrayListOf<Int>()
            if (personTagViewList[1].tag as Boolean) {
                //在线
                list.add(Constant.ONLINE_STATUS_ONLINE)
            }
            if (personTagViewList[2].tag as Boolean) {
                //离线
                list.add(Constant.ONLINE_STATUS_OFFLINE)
            }
            if (personTagViewList[3].tag as Boolean) {
                //定位失败
                list.add(Constant.ONLINE_STATUS_LOCATION_FAIL)
            }
            selectedTagList.put(Constant.OBJECT_TYPE_PERSON, list)
        }
        //物品筛选
        if (objectTagViewList[0].tag as Boolean) {
            //物品选中全部
            selectedTagList.put(
                Constant.OBJECT_TYPE_OBJECT, arrayListOf(
                    Constant.ONLINE_STATUS_ONLINE,
                    Constant.ONLINE_STATUS_OFFLINE,
                    Constant.ONLINE_STATUS_LOCATION_FAIL
                )
            )
        } else {
            val list = arrayListOf<Int>()
            if (objectTagViewList[1].tag as Boolean) {
                //在线
                list.add(Constant.ONLINE_STATUS_ONLINE)
            }
            if (objectTagViewList[2].tag as Boolean) {
                //离线
                list.add(Constant.ONLINE_STATUS_OFFLINE)
            }
            if (objectTagViewList[3].tag as Boolean) {
                //定位失败
                list.add(Constant.ONLINE_STATUS_LOCATION_FAIL)
            }
            selectedTagList.put(Constant.OBJECT_TYPE_OBJECT, list)
        }
        //动物筛选
        if (animalTagViewList[0].tag as Boolean) {
            //物品选中全部
            selectedTagList.put(
                Constant.OBJECT_TYPE_ANIMAL, arrayListOf(
                    Constant.ONLINE_STATUS_ONLINE,
                    Constant.ONLINE_STATUS_OFFLINE,
                    Constant.ONLINE_STATUS_LOCATION_FAIL
                )
            )
        } else {
            val list = arrayListOf<Int>()
            if (animalTagViewList[1].tag as Boolean) {
                //在线
                list.add(Constant.ONLINE_STATUS_ONLINE)
            }
            if (animalTagViewList[2].tag as Boolean) {
                //离线
                list.add(Constant.ONLINE_STATUS_OFFLINE)
            }
            if (animalTagViewList[3].tag as Boolean) {
                //定位失败
                list.add(Constant.ONLINE_STATUS_LOCATION_FAIL)
            }
            selectedTagList.put(Constant.OBJECT_TYPE_ANIMAL, list)
        }
        //其他筛选
        if (otherTagViewList[0].tag as Boolean) {
            //其他选中全部
            selectedTagList.put(
                Constant.OBJECT_TYPE_OTHER, arrayListOf(
                    Constant.ONLINE_STATUS_ONLINE,
                    Constant.ONLINE_STATUS_OFFLINE,
                    Constant.ONLINE_STATUS_LOCATION_FAIL
                )
            )
        } else {
            val list = arrayListOf<Int>()
            if (otherTagViewList[1].tag as Boolean) {
                //在线
                list.add(Constant.ONLINE_STATUS_ONLINE)
            }
            if (otherTagViewList[2].tag as Boolean) {
                //离线
                list.add(Constant.ONLINE_STATUS_OFFLINE)
            }
            if (otherTagViewList[3].tag as Boolean) {
                //定位失败
                list.add(Constant.ONLINE_STATUS_LOCATION_FAIL)
            }
            selectedTagList.put(Constant.OBJECT_TYPE_OTHER, list)
        }
        return selectedTagList
    }

    /**
     * 重置图层菜单的标签状态
     */
    fun resetMapMenuTagStatus() {
        if (!isCreated) return
        allTagViewList.forEach {
            it.forEachIndexed { index, textView ->
                if (index == 0) {
                    textView.setTextColor(R.color.theme.toColorRes())
                    textView.setBackgroundResource(R.drawable.shape_radius5_bg_e3eafe_bd_theme)
                    textView.tag = true
                } else {
                    textView.setTextColor(R.color.color_666666.toColorRes())
                    textView.setBackgroundResource(R.drawable.shape_radius5_bg_f9f9ff)
                    textView.tag = false
                }
            }
        }
    }

    /**
     * 设置有权限的监管对象
     */
    fun setObjectType(objectList: ArrayList<Int>) {
        if (this.objectList != null && this.objectList.toJson() == objectList.toJson()) return
        this.objectList = objectList
        initTagUI()
    }

    /**
     * 初始化权限标签
     */
    private fun initTagUI() {
        if (objectList.isNullOrEmpty() || !isCreated) return
        vehicleLayout.visible(objectList!!.contains(Constant.OBJECT_TYPE_VEHICLE))
        shipLayout.visible(objectList!!.contains(Constant.OBJECT_TYPE_SHIP))
        personLayout.visible(objectList!!.contains(Constant.OBJECT_TYPE_PERSON))
        animalLayout.visible(objectList!!.contains(Constant.OBJECT_TYPE_ANIMAL))
        objectLayout.visible(objectList!!.contains(Constant.OBJECT_TYPE_OBJECT))
        otherLayout.visible(objectList!!.contains(Constant.OBJECT_TYPE_OTHER))
    }

    /**
     * 绑定每组Tag的点击事件
     */
    private fun bindingTagGroupClickListener(textViewList: List<TextView>) {
        textViewList.forEachIndexed { index, textView ->
            textView.setOnClickListener {
                val tag = it.tag as Boolean
                if (index == 0) {
                    //点击的是全部Tag
                    if (tag) {
                        //当前已选中，则取消选中
                        textView.setTextColor(R.color.color_666666.toColorRes())
                        textView.setBackgroundResource(R.drawable.shape_radius5_bg_f9f9ff)
                    } else {
                        //当前未选中，则设置为选中，且将其他Tag都设置为未选中
                        textView.setTextColor(R.color.theme.toColorRes())
                        textView.setBackgroundResource(R.drawable.shape_radius5_bg_e3eafe_bd_theme)
                        textViewList.forEachIndexed { index, textView ->
                            if (index > 0) {
                                textView.setTextColor(R.color.color_666666.toColorRes())
                                textView.setBackgroundResource(R.drawable.shape_radius5_bg_f9f9ff)
                                textView.tag = false
                            }
                        }
                    }
                } else {
                    //普通Tag
                    if (tag) {
                        textView.setTextColor(R.color.color_666666.toColorRes())
                        textView.setBackgroundResource(R.drawable.shape_radius5_bg_f9f9ff)
                    } else {
                        textView.setTextColor(R.color.theme.toColorRes())
                        textView.setBackgroundResource(R.drawable.shape_radius5_bg_e3eafe_bd_theme)
                        val allTextView = textViewList[0]
                        if (allTextView.tag as Boolean) {
                            //将全部Tag取消选中
                            allTextView.setTextColor(R.color.color_666666.toColorRes())
                            allTextView.setBackgroundResource(R.drawable.shape_radius5_bg_f9f9ff)
                            allTextView.tag = false
                        }
                    }
                }
                textView.tag = !tag
                onSelectChangeList.invoke(getSelectedTagList())
            }
        }
    }

    private fun bindingTagSwitchClickListener(isBounds: Boolean, showTextView: TextView, hideTextView: TextView) {
        showTextView.setOnClickListener {
            showTextView.setTextColor(R.color.theme.toColorRes())
            showTextView.setBackgroundResource(R.drawable.shape_radius5_bg_e3eafe_bd_theme)
            showTextView.tag = true
            hideTextView.setTextColor(R.color.color_666666.toColorRes())
            hideTextView.setBackgroundResource(R.drawable.shape_radius5_bg_f9f9ff)
            hideTextView.tag = false
            if (isBounds){
                onPolygonChangeList.invoke(showTextView.tag as Boolean)
            }else{
                onLineChangeList.invoke(showTextView.tag as Boolean)
            }
        }
        hideTextView.setOnClickListener {
            hideTextView.setTextColor(R.color.theme.toColorRes())
            hideTextView.setBackgroundResource(R.drawable.shape_radius5_bg_e3eafe_bd_theme)
            hideTextView.tag = true
            showTextView.setTextColor(R.color.color_666666.toColorRes())
            showTextView.setBackgroundResource(R.drawable.shape_radius5_bg_f9f9ff)
            showTextView.tag = false
            if (isBounds){
                onPolygonChangeList.invoke(showTextView.tag as Boolean)
            }else{
                onLineChangeList.invoke(showTextView.tag as Boolean)
            }
        }
    }

    fun isShowBounds() = boundsShow != null && boundsShow.tag as Boolean

    fun isShowLine() = lineShow != null && lineShow.tag as Boolean

}